<template>
    <!-- 轮播图管理 -->
    <div class="loop-box">
        <!-- 条件查询 -->
        <div>
            <el-card class="box-card">
                <el-form :inline="true" :model="query" size="mini">
                    <el-form-item label="ID">
                        <el-input v-model.trim.trim="query.id" placeholder="请输入ID" clearable></el-input>
                    </el-form-item>
                    <el-form-item label="标题">
                        <el-input v-model.trim.trim="query.title" placeholder="请输入标题" clearable></el-input>
                    </el-form-item>
                    <el-form-item label="是否推荐">
                        <el-select v-model.trim="query.state" clearable style="width: 120px">
                            <!-- 是否推荐:(0 正常；1 推荐) -->
                            <el-option label="正常" :value="1"></el-option>
                            <el-option label="删除" :value="0"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="日期范围">
                        <el-date-picker
                                clearable
                                v-model.trim="query.begin"
                                placeholder="开始时间"
                                value-format="yyyy-MM-dd"/>
                    </el-form-item>
                    <el-form-item label="-">
                        <el-date-picker
                                clearable
                                v-model.trim="query.end"
                                placeholder="结束时间"
                                value-format="yyyy-MM-dd"/>
                    </el-form-item>
                    <el-form-item>
                        <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
                        <el-button icon="el-icon-refresh" @click="reload">重置</el-button>
                        <el-button icon="el-icon-circle-plus-outline" type="success" @click="openAdd">添加轮播图
                        </el-button>
                    </el-form-item>
                </el-form>
            </el-card>
        </div>
        <br>
        <div class="loop-list-box">
            <el-table
                    v-loading="loading"
                    :data="list"
                    border
                    size="mini"
                    style="width: 100%">
                <el-table-column align="center" prop="imageUrl" label="图片" width="60">
                    <template slot-scope="scope">
                        <el-image
                                class="img"
                                :src="scope.row.imageUrl"
                                :preview-src-list="[scope.row.imageUrl]">
                        </el-image>
                    </template>
                </el-table-column>
                <el-table-column
                        fixed
                        prop="id"
                        label="ID"
                        align="center"
                        width="200">
                </el-table-column>
                <el-table-column
                        prop="title"
                        label="标题"
                        align="center"
                        width="150">
                </el-table-column>
                <el-table-column
                        prop="targetUrl"
                        label="目标网址"
                        align="center"
                        width="200">
                </el-table-column>
                <el-table-column
                        prop="sort"
                        label="排序"
                        align="center"
                        width="150">
                </el-table-column>
                <el-table-column
                        prop="state"
                        label="状态"
                        align="center"
                        width="120">
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.state == '1'">正常</el-tag>
                        <el-tag type="danger" v-if="scope.row.state == '0'">已删除</el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="createTime"
                        label="创建时间"
                        align="center"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="updateTime"
                        label="更新时间"
                        align="center"
                        width="180">
                </el-table-column>
                <el-table-column
                        fixed="right"
                        label="操作"
                        align="center"
                        width="150">
                    <template slot-scope="scope">
                        <el-button type="primary" @click="handleEdit(scope.row.id)" size="small">编辑</el-button>
                        <el-button type="danger" v-if="scope.row.state !== '0'" @click="handleDelete(scope.row.id)"
                                   size="small">删除
                        </el-button>
                        <el-button type="danger" v-if="scope.row.state === '0'" disabled
                                   @click="handleDelete(scope.row.id)" size="small">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <!-- 分页组件 -->
        <el-pagination
                align="center"
                background
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="page.current"
                :page-sizes="[10, 20,30,50]"
                :page-size="page.size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="page.total">
        </el-pagination>
        <!-- 子组件: 添加或编辑分类 -->
        <EditLoop
                :title="edit.title"
                :visible="edit.visible"
                :formData="edit.formData"
                :remoteClose="remoteClose"/>
        <EditLoop/>
    </div>
</template>

<script>
    import {getLoopList, deleteLoopById, getLoopById} from '@/api/api'
    import EditLoop from "./EditLoop";

    export default {
        name: "Loop",
        // 注册组件
        components: {
            EditLoop
        },
        data() {
            return {
                loading: false,
                list: [],
                query: {},
                page: {
                    current: 1,
                    size: 10,
                    total: 0
                },
                // 新增或编辑数据
                edit: {
                    title: '',
                    visible: false,
                    formData: {}
                }
            }
        },
        created() {
            this.fetchData()
        },
        methods: {
            // 获取分类列表数据
            async fetchData() {
                this.loading = true
                const {data} = await getLoopList(this.page.current, this.page.size, this.query)
                this.loading = false
                this.list = data.records
                this.page.total = data.total
            },
            // 分页-切换每页显示多少条记录
            handleSizeChange(size) {
                this.page.size = size
                this.fetchData()
            },
            // 分页-切换当前页码
            handleCurrentChange(current) {
                this.page.current = current
                this.fetchData()
            },
            // 条件查询
            queryData() {
                this.page.current = 1
                this.fetchData()
            },
            // 条件查询-重置表单
            reload() {
                this.query = {}
                this.fetchData()
            },
            // 打开添加轮播图窗口
            openAdd() {
                this.edit.visible = true
                this.edit.title = '添加轮播图'
                this.edit.formData = {}
            },
            // 关闭新增或编辑子窗口
            remoteClose() {
                this.edit.visible = false
                this.fetchData()
            },
            // 删除
            handleDelete(id) {
                console.log('id', id)
                this.$confirm('确定删除这条记录吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    // 发送删除请求
                    console.log('发送删除请求 =====')
                    deleteLoopById(id).then(response => {
                        console.log('请求发送完成 ===== ')
                        // 刷新列表数据
                        this.fetchData()
                        console.log('获取数据列表完成 ===== ')
                        // 处理响应结果提示
                        if (response.code === 20000) {
                            console.log('response.code === 20000  =====')
                            this.$message.success(response.message)
                        } else {
                            this.$message.error(response.message)
                        }
                    })
                }).catch(() => {
                    // 取消删除，不用理会
                })
            },
            // 根据ID详情
            async handleEdit(id) {
                const response = await getLoopById(id)
                this.edit.formData = response.data
                this.edit.visible = true
                this.edit.title = '编辑轮播图'
            }
        }
    }
</script>
<style>
    .img {
        width: 30px;
        height: 30px;
        border-radius: 5px;
    }

    .el-pagination {
        padding: 20px;
    }
</style>